//package lt.hsu.security;
//
//import lombok.AllArgsConstructor;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.io.ClassPathResource;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;
//
//import java.security.KeyPair;
//import java.util.ArrayList;
//
///**
// * 认证服务器配置
// * Created by macro on 2020/6/19.
// */
//@AllArgsConstructor
//@Configuration
//@EnableAuthorizationServer
//public class Oauth2ServerConfig extends AuthorizationServerConfigurerAdapter {
//
//    private final PasswordEncoder passwordEncoder;
//    private final SecurityUserDetailService userDetailsService;
//    private final AuthenticationManager authenticationManager;
//    private final JwtTokenEnhancer jwtTokenEnhancer;
//
//    @Override
//    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
//        clients.inMemory()
//                .withClient("client-app")
//                .secret(passwordEncoder.encode("123456"))
//                .scopes("all")
//                .authorizedGrantTypes("password", "refresh_token")
//                .accessTokenValiditySeconds(3600)
//                .refreshTokenValiditySeconds(86400);
//    }
//
//
//    @Override
//    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
//        TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
//        List<TokenEnhancer> delegates = new ArrayList<>();
//        delegates.add(jwtTokenEnhancer);
//        delegates.add(accessTokenConverter());
//        enhancerChain.setTokenEnhancers(delegates); //配置JWT的内容增强器
//        endpoints.authenticationManager(authenticationManager)
//                .userDetailsService(userDetailsService) //配置加载用户信息的服务
//                .accessTokenConverter(accessTokenConverter())
//                .tokenEnhancer(enhancerChain);
//    }
//
//    @Override
//    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
//        security.allowFormAuthenticationForClients();
//    }
//
//    @Bean
//    public JwtAccessTokenConverter accessTokenConverter() {
//        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
//        jwtAccessTokenConverter.setKeyPair(keyPair());
//        return jwtAccessTokenConverter;
//    }
//
//    @Bean
//    public KeyPair keyPair() {
//        //从classpath下的证书中获取秘钥对
//        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(new ClassPathResource("jwt.jks"), "123456".toCharArray());
//        return keyStoreKeyFactory.getKeyPair("jwt", "123456".toCharArray());
//    }
//
//}
